package zwj;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.FactoryConfigurationError;

public class MainApp {

	public static void main(String[] args) throws IOException {

		String path = "D:\\log\\log.log";
		File file = new File(path);
		if (file.exists() && file.isFile()) {
			// main

			FileInputStream fileinputs;

			FileInputStream fileInputs = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputs);
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			// bufferedReader.readerLine();
			String line = null;

			File ipFile = new File("D:\\log\\ips.txt");
			if (!ipFile.exists()) {
				ipFile.createNewFile();

			}

			FileOutputStream fileOutputStream = new FileOutputStream(ipFile);

			while ((line = bufferedReader.readLine()) != null) {
				//System.out.print(MainApp.getIP(line));

				// BufferedOutputStream bufferedOutputStream = new
				// bufferedOutputStream.
				fileOutputStream.write(MainApp.getIP(line).getBytes());
				fileOutputStream.write("\n".getBytes());

				// D:\log\ips.txt

			}
			fileOutputStream.flush();
			fileOutputStream.close();

			 

		} else {
			System.out.println("not a file or not exits!!");
		}
		System.out.println("end");
	}

	public static String getIP(String content) {
		String regUrl = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";

		Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		boolean blnp = m.find();
		if (blnp == true) {// get it
			return m.group();
		}
		return null;
	}
}
